/*
* @Author: kalung.di
* @describe: 多语言系统
* @Date:   2017-03-27 00:33:29
* @Last Modified by:   kalung.di
* @Last Modified time: 2017-03-31 02:29:36
*/

$(function() {

  var langCookie,
    cookieExp,
    langBox,
    parseDom,
    handles,
    bind,
    init; 

  parseDom = function() {
    langCookie = 'US_lang';
    cookieExp = 30;
    langBox = $('#langBox');

  };

  handles  = {
    // 点击切换语言
    clickChange: function(e) {
      var target = e.target;
      var lang = target.dataset.lang;
      $.cookie(langCookie, lang, {expires: cookieExp});
      location.reload();
    },
    // 加载语言
    loadLang: function(data, position, lang) {
      $("[" + data + "]").localize(position, {
        pathPrefix: "js/lang",
        language: lang,
      });
    },
    // 根据浏览器语言切换
    changeByBr: function() {
      var uulanguage = (navigator.language || navigator.browserLanguage).toLowerCase();
      // 先检查浏览器语言
      if (uulanguage.indexOf("en-US") > -1) {
        handles.loadLang('data-localize', 'default', 'en-US');
      } else if (uulanguage.indexOf("zh_CN") > -1) {
        handles.loadLang('data-localize', 'default', 'zh-CN');
      } else {
        handles.loadLang('data-localize', 'default', 'zh-CN');
      }
    },
    changeByCo: function(val) {
        handles.loadLang('data-localize', 'default', val);
      /*if(val == 'zh') {
        console.log(2323)
        handles.loadLang('data-localize', 'default', 'zh_CN');
      }else if(val == 'en') {
        handles.loadLang('data-localize', 'default', 'en_US');
      }else {
        handles.loadLang('data-localize', 'default', 'zh_CN');
      }*/
    },
    // 加载切换语言
    reload: function() {
      var val = $.cookie(langCookie);
      if(val) {
        handles.changeByCo(val);
      }else {
        handles.changeByBr();
      }
    },

  };

  bind = function() {
    langBox.on('click', 'a', handles.clickChange);
    handles.reload();
  };

  init = function() {
    parseDom();
    bind();
  };

  init();

  






});


